1
|
|
|
"use strict"; |
2
|
|
|
|
3
|
|
|
const express = require("express"); |
4
|
|
|
const serveStatic = require("serve-static"); |
5
|
|
|
const path = require("path"); |
|
|
|
|
6
|
|
|
const SSE = require("sse"); |
7
|
|
|
const redis = require("redis"); |
|
|
|
|
8
|
|
|
const amqp = require("amqplib/callback_api"); |
9
|
|
|
|
10
|
|
|
const app = express(); |
11
|
|
|
|
12
|
|
|
// const passport = require('passport') |
13
|
|
|
// const BasicStrategy = require('passport-http').BasicStrategy |
14
|
|
|
// passport.use(new BasicStrategy( |
15
|
|
|
// function(username, password, done) { |
16
|
|
|
// //todo: obviously needs to change |
17
|
|
|
// if (username.valueOf() === "fullcycle" && password.valueOf() === "mining") |
18
|
|
|
// return done(null, true); |
19
|
|
|
// else |
20
|
|
|
// { |
21
|
|
|
// console.log("rejected!") |
22
|
|
|
// return done(null, false); |
23
|
|
|
// } |
24
|
|
|
// } |
25
|
|
|
// )); |
26
|
|
|
|
27
|
|
|
const services = require("./services"); |
28
|
|
|
const messages = require("./messages"); |
|
|
|
|
29
|
|
|
|
30
|
|
|
var api = require("./api.js"); |
31
|
|
|
|
32
|
|
|
function bail(err, conn) { |
33
|
|
|
console.error("bailing..."); |
34
|
|
|
console.error(err); |
35
|
|
|
if (conn) conn.close(function() { |
|
|
|
|
36
|
|
|
// if (doexit) |
37
|
|
|
// process.exit(1); |
38
|
|
|
}); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
//route all other calls to the home page. this is causing "path is not defined" in line 179 |
43
|
|
|
// app.get("/*", function(req, res) { |
44
|
|
|
// res.sendFile(path.join(__dirname, "/index.html"), function(err) { |
45
|
|
|
// if (err) { |
46
|
|
|
// res.status(500).send(err) |
47
|
|
|
// } |
48
|
|
|
// }) |
49
|
|
|
// }); |
50
|
|
|
|
51
|
|
|
//in production this serves up the react bundle |
52
|
|
|
app.use(serveStatic("../web/build") |
53
|
|
|
//, |
54
|
|
|
// passport.authenticate("basic", { session: false }) |
55
|
|
|
); |
56
|
|
|
app.use("/api", api); |
57
|
|
|
var server = app.listen(services.web.port, () => console.log(`Listening on port ${services.web.port}`)); |
|
|
|
|
58
|
|
|
function onWebError(error) { |
59
|
|
|
if (error.syscall !== "listen") { |
60
|
|
|
throw error; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
//var bind = typeof port === "string" ? "Pipe " + port : "Port " + port; |
64
|
|
|
var bind = services.web.port; |
65
|
|
|
|
66
|
|
|
// handle specific listen errors with friendly messages |
67
|
|
|
switch (error.code) { |
68
|
|
|
case "EACCES": |
69
|
|
|
console.error(error.code + ":" + bind + " requires elevated privileges"); |
70
|
|
|
process.exit(1); |
|
|
|
|
71
|
|
|
break; |
72
|
|
|
case "EADDRINUSE": |
73
|
|
|
console.error(error.code + ":" + bind + " is already in use. Close the other app and try again"); |
74
|
|
|
process.exit(1); |
|
|
|
|
75
|
|
|
break; |
76
|
|
|
default: |
77
|
|
|
throw error; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
server.on("error", onWebError); |
81
|
|
|
|
82
|
|
|
var busConnect = null; |
83
|
|
|
|
84
|
|
|
function on_connect(err, conn) { |
85
|
|
|
if (err !== null) return bail(err); |
|
|
|
|
86
|
|
|
process.once("SIGINT", function() { conn.close(); }); |
87
|
|
|
|
88
|
|
|
busConnect = conn; |
|
|
|
|
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
//set up the full cycle alerts feed to send alerts to the browser |
93
|
|
|
var sse = new SSE(server); |
94
|
|
|
sse.on("connection", function (sseConnection) { |
95
|
|
|
//console.log("new sse connection"); |
96
|
|
|
|
97
|
|
|
const qAlert = "alert"; |
98
|
|
|
let alertChannel = null; |
99
|
|
|
|
100
|
|
|
function alertMessage(msg) { |
101
|
|
|
if (msg) { |
102
|
|
|
//msg.content.toString() |
103
|
|
|
//console.log(" [x] '%s'", "received alert message"); |
104
|
|
|
sseConnection.send({ |
105
|
|
|
event: "full-cycle-alert", |
106
|
|
|
data: msg.content.toString() |
107
|
|
|
}); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
function on_channel_open_alert(err, ch) { |
112
|
|
|
if (err !== null) { return bail(err, busConnect); } |
113
|
|
|
alertChannel = ch; |
114
|
|
|
ch.on("error", function (err) { |
|
|
|
|
115
|
|
|
//console.error(err); |
116
|
|
|
//console.log("channel Closed"); |
117
|
|
|
}); |
118
|
|
|
ch.assertQueue("", {exclusive: true}, function(err, ok) { |
119
|
|
|
var q = ok.queue; |
120
|
|
|
ch.bindQueue(q, qAlert, ""); |
121
|
|
|
ch.consume(q, alertMessage, {noAck: true}, function(err, ok) { |
|
|
|
|
122
|
|
|
if (err !== null) return bail(err, busConnect); |
|
|
|
|
123
|
|
|
//console.log(" [*] Waiting for alert. To exit press CTRL+C."); |
124
|
|
|
}); |
125
|
|
|
}); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
const qMiner = "statisticsupdated"; |
129
|
|
|
let miner_channel = null; |
130
|
|
|
|
131
|
|
|
function minerMessage(msg) { |
132
|
|
|
if (msg) { |
133
|
|
|
//msg.content.toString() |
134
|
|
|
//console.log(" [x] '%s'", "received miner message"); |
135
|
|
|
sseConnection.send({ |
136
|
|
|
event: "full-cycle-miner", |
137
|
|
|
data: msg.content.toString() |
138
|
|
|
}); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
function on_channel_open_miner(err, ch) { |
143
|
|
|
if (err !== null) return bail(err, busConnect); |
|
|
|
|
144
|
|
|
miner_channel = ch; |
145
|
|
|
ch.on("error", function (err) { |
146
|
|
|
console.error(err); |
147
|
|
|
//console.log("miner channel Closed"); |
148
|
|
|
}); |
149
|
|
|
ch.assertQueue("", {exclusive: true}, function(err, ok) { |
150
|
|
|
var q = ok.queue; |
151
|
|
|
ch.bindQueue(q, qMiner, ""); |
152
|
|
|
ch.consume(q, minerMessage, {noAck: true}, function(err, ok) { |
|
|
|
|
153
|
|
|
if (err !== null) return bail(err, busConnect); |
|
|
|
|
154
|
|
|
//console.log(" [*] Waiting for miner stats. To exit press CTRL+C."); |
155
|
|
|
}); |
156
|
|
|
}); |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
const qSensor = "sensor"; |
160
|
|
|
let sensor_channel = null; |
161
|
|
|
function sensorMessage(msg) { |
162
|
|
|
if (msg) { |
163
|
|
|
//msg.content.toString() |
164
|
|
|
//console.log(" [x] '%s'", "received sensor message"); |
165
|
|
|
sseConnection.send({ |
166
|
|
|
event: "full-cycle-sensor", |
167
|
|
|
data: msg.content.toString() |
168
|
|
|
}); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
function on_channel_open_sensor(err, ch) { |
173
|
|
|
if (err !== null) return bail(err, busConnect); |
|
|
|
|
174
|
|
|
sensor_channel = ch; |
175
|
|
|
ch.on("error", function (err) { |
176
|
|
|
console.error(err) |
177
|
|
|
//console.log("sensor channel Closed"); |
178
|
|
|
}); |
179
|
|
|
ch.assertQueue("", {exclusive: true}, function(err, ok) { |
180
|
|
|
var q = ok.queue; |
181
|
|
|
ch.bindQueue(q, qSensor, ""); |
182
|
|
|
ch.consume(q, sensorMessage, {noAck: true}, function(err, ok) { |
|
|
|
|
183
|
|
|
if (err !== null) return bail(err, busConnect); |
|
|
|
|
184
|
|
|
//console.log(" [*] Waiting for sensor. To exit press CTRL+C."); |
185
|
|
|
}); |
186
|
|
|
}); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if (busConnect) |
190
|
|
|
{ |
191
|
|
|
busConnect.createChannel(on_channel_open_alert); |
192
|
|
|
busConnect.createChannel(on_channel_open_miner); |
193
|
|
|
busConnect.createChannel(on_channel_open_sensor); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
sseConnection.on("close", function () { |
197
|
|
|
//console.log("lost sse connection"); |
198
|
|
|
if (alertChannel) alertChannel.close(); |
|
|
|
|
199
|
|
|
if (miner_channel) miner_channel.close(); |
|
|
|
|
200
|
|
|
if (sensor_channel) sensor_channel.close(); |
|
|
|
|
201
|
|
|
}); |
202
|
|
|
|
203
|
|
|
}); |
204
|
|
|
|
205
|
|
|
try { |
206
|
|
|
amqp.connect(services.messagebus.connection, on_connect); |
207
|
|
|
} |
208
|
|
|
catch(error) { |
209
|
|
|
console.error(error); |
210
|
|
|
} |
211
|
|
|
|